#include "basis.h"
#include "json.hpp"

options::options() : options::options(9, Vcycle, Dimension::One, 10, 10, BoundType::Dirichlet, Operators::Restriction::Injection, Operators::Linear, 100)
{
}

options::options(int degree, CycleChoice cycleChoice, Dimension dim, int nu1, int nu2, BoundType boundType, Operators::Restriction rtype, Operators::Interpolation itype, int maxIteration) : degree(degree), cycleChoice(cycleChoice), dim(dim), nu1(nu1), nu2(nu2), boundType(boundType), rtype(rtype), itype(itype), maxIteration(maxIteration)
{
}

bool options::load(const std::string &path)
{
    std::ifstream file(path);
    if (!file.is_open())
        return false;
    nlohmann::json data;
    file >> data;
    file.close();
    degree = data["degree"];
    cycleChoice = string2CycleChoice(data["cycle_choice"]);
    dim = string2Dimension(data["dimension"]);
    nu1 = data["nu1"];
    nu2 = data["nu2"];
    boundType = string2BoundType(data["bound_type"]);
    rtype = Operators::string2Restriction(data["restriction"]);
    itype = Operators::string2Interpolation(data["interpolation"]);
    maxIteration = data["max_iteration"];
    return true;
}

options options::LoadFromFile(const std::string &path)
{
    options tmp;
    tmp.load(path);
    return tmp;
}

nlohmann::json options::serialize()
{
    nlohmann::json data;
    data["degree"] = degree;
    data["cycle_choice"] = CycleChoice2string(cycleChoice);
    data["dimension"] = Dimension2string(dim);
    data["nu1"] = nu1;
    data["nu2"] = nu2;
    data["bound_type"] = BoundType2string(boundType);
    data["restriction"] = Operators::Restriction2string(rtype);
    data["interpolation"] = Operators::Interpolation2string(itype);
    data["max_iteration"] = maxIteration;
    return data;
}
